using Microsoft.AspNetCore.Mvc;
using TMS.Application.Common.Interface;
using TMS.Application.Utils;
using TMS.Domain.Entity;

namespace TMS.Api.Controllers
{
    [ApiController]
    public class AStudentController : ControllerBase
    {
        private readonly IRepository<College> _College;

        private readonly IRepository<Major> _Major;
        private readonly IRepository<Class> _Class;
        private readonly IIdentityService _IdentityService;

        private readonly IRepository<Students> _Students;
        private readonly IRepository<Book> _Book;
        private readonly IRepository<Term> _Term;
        private readonly IRepository<Mark> _Mark;
        private readonly IRepository<Course> _Course;

        private readonly IRepository<Subject> _Subject;
        private readonly IRepository<Satisfaction> _Satisfaction;
        private readonly IRepository<Teachers> _Teachers;



        public AStudentController(
            IRepository<Teachers> Teachers,
            IIdentityService IdentityService,
            IRepository<College> College,
            IRepository<Major> Major,
             IRepository<Class> Class,
            IRepository<Students> Students,
            IRepository<Book> Book,
            IRepository<Term> Term,
             IRepository<Mark> Mark,
                     IRepository<Course> Course,
            IRepository<Subject> Subject,
            IRepository<Satisfaction> Satisfaction

        )
        {
            _Teachers = Teachers;
            _Satisfaction = Satisfaction;
            _Subject = Subject;
            _Course = Course;
            _Mark = Mark;
            _Term = Term;
            _Book = Book;
            _Class = Class;
            _Major = Major;
            _College = College;
            _Students = Students;
            _IdentityService = IdentityService;
        }

        [HttpPost("/AddScore")]
        public async Task<object> AddScore(Course Course)
        {
            try
            {
                await _Course.AddAsync(Course);
                return new
                {
                    code = 200,
                    msg = "添加成功",
                    data = ""
                };
            }
            catch (System.Exception)
            {

                return new
                {
                    code = 500,
                    msg = "添加错误",
                    data = ""
                };
            }

        }

        [HttpGet("/CheckScore/{StudentId}/{termId}")]

        public async Task<object> CheckScore(Guid StudentId, Guid termId)
        {
            try
            {

                List<object> ScorecList = new List<object>();

                //学生信息
                var Student = _Students.Table.Where(x => x.Id == StudentId).FirstOrDefault();
                //成绩列表
                //成绩表可能要查学生的id这样的话才能查到这个学生的所有成绩
                var MarkLisat = _Mark.Table.Where(x => x.StudentId == Student.StudentId).ToList();
                //教师表
                var teacherList = _Teachers.Table.ToList();
                //科目列表
                var SubjectList = _Subject.Table.ToList();

                var CourseList = new CourseList(
                 _IdentityService,
                 _Class,
                 _Students,
                 _Term,
                 _Course,
                 _Subject
                );
                //所有学期所有班级的课程的科目
                var CourseLists = CourseList.Courses(termId,Student.ClassId).ToList();
                //查找出某学期某个班级的课表
                var class_Course = CourseLists.Where(x => x.ClassId == Student.ClassId && x.TermId == termId).ToList();
                foreach (var item in class_Course)
                {
                    try
                    {
                        //科目信息
                        var subject = SubjectList.Where(x => x.Id == item.SubjectId).FirstOrDefault();
                        //科目名字
                        var SubjectName = item.SubjectName;
                        //教师信息
                        var teacher = teacherList.Where(x => x.TeacherId == item.TeacherId).FirstOrDefault();
                        //教师姓名
                        var TeacherName = teacher.TeacherName;
                        // 成绩信息
                        var Mark = MarkLisat.Where(x => x.Course == item.SubjectId).FirstOrDefault();
                        //科目成绩
                        var Score = Mark.Score;
                        ScorecList.Add(new
                        {
                            SubjectName,
                            TeacherName,
                            Score
                        }
                        );
                    }
                    catch (System.Exception)
                    {
                        //课表的老师可能有错误
                    }
                }

                return new
                {
                    code = 200,
                    msg = "查找完成",
                    data = ScorecList
                };
            }
            catch (System.Exception)
            {

                return new
                {
                    code = 500,
                    msg = "删除错误",
                    data = ""
                };
            }






        }


        [HttpGet("/DelAllScore")]

        public async Task<object> DelAllScore()
        {
            try
            {
                //删除课表
                var CourseList = _Course.Table.ToList();
                await _Course.DeleteBulkAsync(CourseList, true);
                return new
                {
                    code = 200,
                    msg = "删除成功",
                    data = ""
                };

            }
            catch (System.Exception)
            {

                return new
                {
                    code = 500,
                    msg = "删除错误",
                    data = ""
                };
            }

        }

        [HttpGet("/ViewSatisfaction/{StudentId}/{termId}")]
        public async Task<object> ViewSatisfaction(Guid StudentId, Guid termId)
        {
            try
            {

                List<object> ViewSatisfaction = new List<object>();
                //学生信息
                var Student = _Students.Table.Where(x => x.Id == StudentId).FirstOrDefault();

                //教师表
                var teacherList = _Teachers.Table.ToList();
                //科目列表
                var SubjectList = _Subject.Table.ToList();

                var CourseList = new CourseList(
                 _IdentityService,
                 _Class,
                 _Students,
                 _Term,
                 _Course,
                 _Subject
                );
                //所有学期所有班级的课程的科目
                var CourseLists = CourseList.Courses(termId,Student.ClassId).ToList();
                //查找出某学期某个班级的课表
                var class_Course = CourseLists.Where(x => x.ClassId == Student.ClassId && x.TermId == termId).ToList();
                foreach (var item in class_Course)
                {
                    try
                    {
                        //科目信息
                        var subject = SubjectList.Where(x => x.Id == item.SubjectId).FirstOrDefault();
                        //科目名字
                        var SubjectName = item.SubjectName;
                        //教师信息
                        var teacher = teacherList.Where(x => x.TeacherId == item.TeacherId).FirstOrDefault();
                        //教师姓名
                        var TeacherName = teacher.TeacherName;
                        ViewSatisfaction.Add(new
                        {
                            SubjectName,
                            TeacherName,
                            teacherId = teacher.Id
                        }
                        );
                    }
                    catch (System.Exception)
                    {
                        //课表的老师可能有错误
                    }
                }

                return new
                {
                    code = 200,
                    msg = "查找完成",
                    data = ViewSatisfaction
                };
            }
            catch (System.Exception)
            {

                return new
                {
                    code = 500,
                    msg = "删除错误",
                    data = ""
                };
            }

        }


        [HttpGet("/ViewTeacherSatisfaction/{TeacherId}/{termId}")]
        public async Task<object> ViewTeacherSatisfaction(string TeacherId, Guid termId)
        {
            //先使用教师id和学期id查找到教学过班级的课程列表，但是可能有重复的班级需要去重
            //然后使班级的id获取班级的信息列表，班级信息列表，
            // 使用班级的id获取满意度调查的分数然后进行平均数计算
            // 然后返回的数据的字段是 学院名称，专业名称，班级名称，班级id，满意度平均分

            // 难点：去重和平均分计算 不过Lambda可能有去重的方法 但是没有尝试过所以如果没有的话需要人工去重有点麻烦,


            try
            {
                List<object> TeacherSatisfactionList = new List<object>();

                var CourseList = new CourseList(
                 _IdentityService,
                 _Class,
                 _Students,
                 _Term,
                 _Course,
                 _Subject
                );
                //查找某学期某老师教的所有班级的课程的科目
                var CourseListes = CourseList.Courses(termId).ToList();
                var CourseLists = CourseListes.Where(x => x.TeacherId == TeacherId).ToList();
                //去重剩下班级id
                var TeachingClass = CourseLists.GroupBy(x => x.ClassId).ToList();
                //班级列表
                var ClassList = _Class.Table.ToList();
                //学生列表
                var StudentList = _Students.Table.ToList();
                //专业列表
                var MajorList = _Major.Table.ToList();
                //学院列表
                var CollegeList = _College.Table.ToList();
                // 满意度列表
                var SatisfactionList = _Satisfaction.Table.ToList();

                foreach (var item in TeachingClass)
                {
                    //班级id
                    var ClassId = item.Key;
                    //班级信息
                    var Class = ClassList.Where(x => x.Id == ClassId).FirstOrDefault();
                    // 班级人数
                    var Class_student_sum = StudentList.Where(x => x.ClassId == Class.Id).ToList().Count;
                    //专业信息
                    var Major = MajorList.Where(x => x.Id == Class.MajorId).FirstOrDefault();
                    //学院信息
                    var College = CollegeList.Where(x => x.Id == Major.CollegeId).FirstOrDefault();
                    //班级的满意度列表
                    var Class_SatisfactionList = SatisfactionList.Where(x => x.ClassId == ClassId).ToList();
                    Double sum = 0;
                    if (Class_SatisfactionList.Count != 0)
                    {
                        //平均分
                        sum = Class_SatisfactionList.Average(x => x.Score);
                    }

                    TeacherSatisfactionList.Add(
                        new
                        {
                            CollegeName = College.CollegeName,
                            MajorName = Major.MajorName,
                            ClassName = Class.ClassName,
                            ClassId = Class.Id,
                            StudentSum = Class_student_sum,
                            SatisfactionSum = sum,
                        }
                    );

                }
                return new
                {
                    code = 200,
                    msg = "查找完成",
                    data = TeacherSatisfactionList
                };
            }
            catch (System.Exception)
            {

                return new
                {
                    code = 500,
                    msg = "查找错误",
                    data = ""
                };
            }


        }



        [HttpGet("/ViewComment/{TeacherId}/{ClassId}")]
        public async Task<object> ViewComment(Guid TeacherId, Guid ClassId)
        {
            //使用班级id与教师id 来查找他的评论然后返回给前端
            // 然后返回的数据的字段是 满意度评论

            try
            {
                var Comment = _Satisfaction.Table.Where(x => x.ClassId == ClassId && x.TeacherId == TeacherId).Select(x => new { x.Advice }).ToList();
                // var CourseList = new CourseList(
                //  _IdentityService,
                //  _Class,
                //  _Students,
                //  _Term,
                //  _Course,
                //  _Subject
                // );
                // //所有学期所有班级的课程的科目
                // var CourseLists = CourseList.Courses().ToList();

                return new
                {
                    code = 200,
                    msg = "查询成功",
                    data = Comment
                };
            }
            catch (System.Exception)
            {

                return new
                {
                    code = 500,
                    msg = "查询错误",
                    data = ""
                };
            }

        }

        [HttpPost("/AddSatisfaction")]
        public async Task<object> AddSatisfaction(Satisfaction Satisfaction)
        {
            try
            {
                //满意度列表
                var SatisfactionList = _Satisfaction.Table.ToList();
                //判断学生id和教师id是否重复
                var student_Satisfaction = SatisfactionList.Where(x => x.StudentId == Satisfaction.StudentId && x.TeacherId == Satisfaction.TeacherId).FirstOrDefault();
                if (student_Satisfaction == null)
                {
                    await _Satisfaction.AddAsync(new Satisfaction
                    {
                        StudentId = Satisfaction.StudentId,
                        ClassId = Satisfaction.ClassId,
                        TeacherId = Satisfaction.TeacherId,
                        Score = Satisfaction.Score,
                        Advice = Satisfaction.Advice,
                        IsWrite = true
                    });

                    return new
                    {
                        code = 200,
                        msg = "添加成功",
                        data = ""
                    };
                }
                else
                {
                    return new
                    {
                        code = 400,
                        msg = "该教师已填写",
                        data = ""
                    };
                }

            }
            catch (System.Exception)
            {

                return new
                {
                    code = 500,
                    msg = "添加错误",
                    data = ""
                };
            }

        }

        [HttpGet("/View")]
        public async Task<object> View()
        {
            var CourseList = new CourseList(
            _IdentityService,
            _Class,
            _Students,
            _Term,
            _Course,
            _Subject
            );
            //所有学期所有班级的课程的科目
            var CourseLists = CourseList.Courses().ToList();
            return CourseLists;
        }

        //根据学生id和班级id查询所有课程
        [HttpGet("/View/{StudentId}/{ClassId}")]
        public async Task<object> ViewCourse(Guid StudentId)
        {
            //根据学生id查找到班级id
            var Student = _Students.Table.Where(x => x.Id == StudentId).FirstOrDefault();
            if (Student != null)
            {
                var CourseList = new CourseList(
                    _IdentityService,
                    _Class,
                    _Students,
                    _Term,
                    _Course,
                    _Subject
           );
                var Course = CourseList.Courses_Class(Student.ClassId).FirstOrDefault(x=>x.ClassId==Student.ClassId);
                if (Course != null)
                {
                    return new
                    {
                        code = 200,
                        data = Course,
                        msg = "查询成功"
                    };
                }
                else
                {
                    return new
                    {
                        code = 400,
                        data = "",
                        msg = "查询失败"
                    };
                }
            }
            else
            {
                return new
                {
                    code = 400,
                    data = "",
                    msg = "不存在该学生"
                };
            }
        }














    }
}